<?php
namespace app\models;

use yii\db\ActiveRecord;


class ShopRecord extends ActiveRecord
{
       /**
     * @inheritdoc
     */
      public static function tableName(){
           return "{{%shop_record}}";
         }

       /**
     * @inheritdoc
     */
      public function rules(){
           return [
                [['openid'],'required'],
                [['openid'],'string','max' => 250],
                [['transTime','useTime'],'safe'],
             ];
          }

     /**
     * @inheritdoc
     */
    public function attributeLabels(){
          return[
              'id' => '兑换序号',
              'commodityId' => '商品id',
              'openid' => '用户openid',
              'image' => '商品图片',
              'name' => '商品名字',
              'transTime' => '兑换时间',
              'useTime' => '使用时间',
              'state' => '状态',
       ];
}

   public function judgeOpenCid($data){
       $openid = $data['openid'];
       $cid = $data['cid'];
       $data = ShopRecord::find()->andWhere('openid = :openid',[':openid' =>$openid])->andWhere('commodityId = :cid',[':cid'=>$cid])->asArray()->one();
         return $data;
   }

   public static function add($data){
          $record= new ShopRecord();
          if($data){
             date_default_timezone_set('PRC');
             $time = date('Y-m-d');
             $record->commodityId = $data['commodityId'];
             $record->image = $data['image'];
             $record->openid = $data['openid'];
             $record->name = $data['name'];
             $record->transTime = $time;
             $record->soldoutTime = $data['soldoutTime'];
             $record->useTime = $time;
             $result = $record->save();
             if($result){
	           $result = 1;
	}else{
		   $result = -1;
          }
        }else{
             $result = -1;      
   }
             return $result;
}

  public function stateUpdate($data){
         date_default_timezone_set('PRC');
         $time = date('Y-m-d'); 
         $key = [
             'id' => $data['id'],
             'openid' => $data['openid'],
         ];
         $changedata = [
             'state' => '0',
             'useTime' => $time,
         ];
         $result = ShopRecord::updateAll($changedata,$key);
         if($result){
             $result = 1;
        }else{  
             $result = -1;
          }              
             return $result;
     }
}

